package com.per.yyyFrame.common;

import com.per.yyyFrame.config.BizExceptional;
import com.per.yyyFrame.config.DruidInit;
import com.per.yyyFrame.enums.SqlType;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: yyy
 * Date: 2022/2/17
 * Time: 15:45
 * Description:
 */
@Service
public class CrudOperation implements InvocationHandler{
  @Override
  public Object execute(String sql,List<String> fileList, List paramList) throws SQLException {
   char[] charList= sql.toCharArray();
   StringBuffer sqlType=new StringBuffer();
   List<Map> finalList=new LinkedList();
   for(int i=0;i<6;i++){
     if(Integer.valueOf(charList[i])!=32){
       sqlType.append(charList[i]);
     }
   }
   if(charList.length<6){
     throw new BizExceptional("请尽量不要在第一个单词前携带空格");
   }

      SqlType type= SqlType.getName(sqlType.toString());
      String finalsql= builderSql(sql, paramList,type);
      Connection conn=DruidInit.mySource.getConnection();

      switch (type){
          case DELETE:
              return builderDelete(conn,finalsql);//删除
          case SELECT:
              return builderSelect(conn,finalsql,fileList,paramList);//查询
          case INSERT:
              return builderInsert(conn,finalsql);//新增
          case UPDATE:
              return builderUpdate(conn,finalsql);//新增
          default:

      }
      return null;
  }

  public String builderSql(String sql,List param,SqlType type){
      char[] sqlChar= sql.toCharArray();
      int placeholderNum=0;
      for(char ch:sqlChar){
          if(Integer.valueOf(ch)==63){
              placeholderNum++;
          }
      }
      if(placeholderNum!=param.size()){
          throw new BizExceptional("参数与占位符数目不一致");
      }

    for(Object obj:param){
        System.out.println(sql);
        Object finalParam;
        if(obj instanceof Date||obj instanceof Integer){
            finalParam=obj;
        }else{
            finalParam="'"+obj+"'";
        }
        if(type.name().equals("INSERT")){
            sql= sql.replaceFirst("\\?", finalParam.toString());
        }else{
            sql= sql.replaceFirst("\\?", finalParam.toString());
        }
    }
      System.out.println("sql----------:"+sql);
    return sql;
  }



  public Object builderSelect(Connection conn,String finalsql,List<String> fileList,List paramList) throws SQLException {
      Statement stmt=conn.createStatement();
      List<Map> finalList=new LinkedList();
      ResultSet rs= stmt.executeQuery(finalsql);//查询
      while (rs.next()){
          Map finalMap=new HashMap();
          for(String str:fileList){
              finalMap.put(str,rs.getObject(str));
          }
          finalList.add(finalMap);
      }
      return finalList;
  }


  public Object builderDelete(Connection conn,String finalsql) throws SQLException {
      PreparedStatement ptmt = conn.prepareStatement(finalsql);
      ptmt.execute();
      int i= ptmt.getUpdateCount();
      if(i>0){
          return "success";
      }
      return "error";
  }



  public Object builderInsert(Connection conn,String finalsql) throws SQLException {
      PreparedStatement ptmt = conn.prepareStatement(finalsql);
      ptmt.execute();
      int i= ptmt.getUpdateCount();
      if(i>0){
          return "success";
      }
      return "error";
  }


  public Object builderUpdate(Connection conn,String finalsql) throws SQLException {
      PreparedStatement ptmt = conn.prepareStatement(finalsql);
      ptmt.execute();
      int i= ptmt.getUpdateCount();
      if(i>0){
         return "success";
      }
       return "error";
  }
}
